//
//  XKTradeMyDealCell.m
//  XKJY
//
//  Created by xuyaguang on 2017/1/13.
//  Copyright © 2017年 xiaokang100. All rights reserved.
//

#import "XKTradeMyDealCell.h"


@interface XKTradeMyDealCell ()
@property (nonatomic, strong) UILabel *timeLabel;
@property (nonatomic, strong) UILabel *typeLabel;
@property (nonatomic, strong) UILabel *priceLabel;
@property (nonatomic, strong) UILabel *numLabel;
@property (nonatomic, strong) UILabel *totalLael;

@property (nonatomic, strong) UILabel *lineBottom;
@end
@implementation XKTradeMyDealCell

#pragma mark - 自定义cell重写构造方法
- (instancetype)initWithStyle:(UITableViewCellStyle)style reuseIdentifier:(NSString *)reuseIdentifier {
    self = [super initWithStyle:style reuseIdentifier:reuseIdentifier];
    if (self) {
        [self configArguments];
        [self createSubviews];
    }
    return self;
}

#pragma mark - 设置自身
- (void)configArguments {
    
}

#pragma mark - 创建控件
- (void)createSubviews {
    
    _timeLabel = [UILabel getLabWithText:@"--.--.--" textFont:Font_14 textColor:BlackColor textAlignment:NSTextAlignmentLeft];
    
    _typeLabel = [UILabel getLabWithText:@"------" textFont:Font_14 textColor:BlackColor textAlignment:NSTextAlignmentCenter];
    
    _priceLabel = [UILabel getLabWithText:@"---.--" textFont:Font_14 textColor:BlackColor textAlignment:NSTextAlignmentCenter];
    
    _numLabel = [UILabel getLabWithText:@"----.--" textFont:Font_14 textColor:BlackColor textAlignment:NSTextAlignmentCenter];
    
    _totalLael = [UILabel getLabWithText:@"----.--" textFont:Font_14 textColor:BlackColor textAlignment:NSTextAlignmentRight];
    
    _lineBottom = [UILabel new];
    _lineBottom.backgroundColor = kBaseGrayColor;

    
    
    [self.contentView addSubview:self.timeLabel];
    [self.contentView addSubview:self.typeLabel];
    [self.contentView addSubview:self.priceLabel];
    [self.contentView addSubview:self.numLabel];
    [self.contentView addSubview:self.totalLael];
    [self.contentView addSubview:self.lineBottom];
    
    
    [self addConstraints];
}


#pragma mark - 添加约束
- (void)addConstraints {
    
    
    
    [self.timeLabel mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.mas_equalTo(self.contentView).offset(22*Scale_W);
        make.top.mas_equalTo(self.contentView).offset(5*Scale_H);
        make.width.mas_equalTo(Main_Width/6);
    }];
    
    [self.lineBottom mas_makeConstraints:^(MASConstraintMaker *make) {
        make.height.mas_equalTo(1*Scale_H);
        make.top.mas_equalTo(self.timeLabel.mas_bottom).offset(5*Scale_H);
        make.left.right.bottom.mas_equalTo(self.contentView).priorityHigh();
    }];
    
    [self.typeLabel mas_makeConstraints:^(MASConstraintMaker *make) {
        make.centerY.mas_equalTo(self.timeLabel);
        make.left.mas_equalTo(self.timeLabel.mas_right).offset(10*Scale_W);
        make.width.mas_equalTo(Main_Width/6*0.7);
    }];
    
    [self.priceLabel mas_makeConstraints:^(MASConstraintMaker *make) {
        make.centerY.mas_equalTo(self.timeLabel);
        make.left.mas_equalTo(self.typeLabel.mas_right).offset(10*Scale_W);
        make.width.mas_equalTo(Main_Width/7);
    }];
    
    [self.numLabel mas_makeConstraints:^(MASConstraintMaker *make) {
        make.centerY.mas_equalTo(self.timeLabel);
        make.left.mas_equalTo(self.priceLabel.mas_right).offset(10*Scale_W);
        make.width.mas_equalTo(Main_Width/5);
    }];
    
    [self.totalLael mas_makeConstraints:^(MASConstraintMaker *make) {
        make.centerY.mas_equalTo(self.timeLabel);
        make.right.mas_equalTo(self.contentView).offset(-10*Scale_W);
    }];
}

- (void)setUserTradeModel:(XKUserTradeModel *)userTradeModel {
    _userTradeModel = userTradeModel;
    
    if ([userTradeModel.type isEqualToString:@"buy"]) {
        self.typeLabel.text = @"买入";
        self.typeLabel.textColor = UIColorFromRGB(0xFD5251);
    } else if ([userTradeModel.type isEqualToString:@"sell"]) {
        self.typeLabel.text = @"卖出";
        self.typeLabel.textColor = UIColorFromRGB(0x4DB54F);
    }
    
    self.priceLabel.text = [NSString changeNumberFormat:userTradeModel.price];
    self.numLabel.text = [NSString changeNumberFormat:NSNumberFormatterDecimalStyle withNumber:[userTradeModel.trade_num componentsSeparatedByString:@"."].firstObject];
    
    
    NSDate *currentTime = [NSDate dateWithTimeIntervalSince1970:[userTradeModel.trade_time intValue]];
    if ([currentTime isToday]) {
        self.timeLabel.text = [NSString getTimeStringWithTimestamp:[userTradeModel.trade_time integerValue] withFormatter:@"HH:mm:ss"];// YY-MM-dd HH:mm:ss
    } else {
        self.timeLabel.text = [NSString getTimeStringWithTimestamp:[userTradeModel.trade_time integerValue] withFormatter:@"YY/MM/dd"];// YY-MM-dd HH:mm:ss
    }
    
    NSString *totalLabelText = [NSString calculateNumberOne:userTradeModel.price calculateType:CalculateType_Multiply numberTwo:userTradeModel.trade_num withMode:NSRoundPlain scale:2];
    self.totalLael.text = [NSString changeNumberFormat:totalLabelText];
}


@end
